package svd;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class SVDTools {
	
	public static String trainFilePath = "/Users/macpro724/Desktop/BigML/proj/track2/training.txt";
	public static String queryTrainPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/query";
	public static String userTrainPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/user";
	public static String titleTrainPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/title";
	public static String clickTrainPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/click";
	public static String yTrainPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/y";
	
	public static String query_userPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/train/q_u";
	public static String query_titlePath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/train/q_t";
	public static String user_titlePath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/train/u_t";
	public static String qutPath = "/Users/macpro724/Desktop/BigML/proj/version2/svd/train/u_t";
	
	public static void generateTrainFile(){
		try{
			BufferedReader trainbr = new BufferedReader(new FileReader(trainFilePath));
			BufferedWriter querybw = new BufferedWriter(new FileWriter(queryTrainPath));
			BufferedWriter userbw = new BufferedWriter(new FileWriter(userTrainPath));
			BufferedWriter titlebw = new BufferedWriter(new FileWriter(titleTrainPath));
			BufferedWriter clickbw = new BufferedWriter(new FileWriter(clickTrainPath));
			BufferedWriter ybw = new BufferedWriter(new FileWriter(yTrainPath));
			
			String line;
			int lineNum = 0;
			while ((line = trainbr.readLine()) != null){
				if (lineNum % 1000000 == 0)
					System.out.println(lineNum);
				lineNum++;
				String[] paras = line.split("\t");// id seg1|seg2|seg3....
				double click = Double.parseDouble(paras[0]);
				double impression = Double.parseDouble(paras[1]);
				int queryID = Integer.parseInt(paras[7]);
				int titleID = Integer.parseInt(paras[9]);
				int userID = Integer.parseInt(paras[11]);
				
				ybw.write(String.valueOf(click/impression));
				ybw.write("\n");
				
				clickbw.write(String.valueOf(click));
				clickbw.write("\t");
				clickbw.write(String.valueOf(impression));
				clickbw.write("\n");
				
				querybw.write(String.valueOf(queryID));
				querybw.write("\n");
				
				titlebw.write(String.valueOf(titleID));
				titlebw.write("\n");
				
				userbw.write(String.valueOf(userID));
				userbw.write("\n");
			}
			
			trainbr.close();
			querybw.close();
			userbw.close();
			titlebw.close();
			clickbw.close();
			ybw.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void generateTrainCombine(String path1, String path2, String outputPath){
		try{
			BufferedReader br1 = new BufferedReader(new FileReader(path1));
			BufferedReader br2 = new BufferedReader(new FileReader(path2));
			BufferedReader cbr = new BufferedReader(new FileReader(clickTrainPath));
			
			HashMap<String, Integer> clickInfo = new HashMap<String, Integer>();
			HashMap<String, Integer> impInfo = new HashMap<String, Integer>();
			
			String lineClick;
			String line1;
			String line2;
			String token;
			int lineNum = 0;
			while ((lineClick = cbr.readLine()) != null){
				if (lineNum % 1000000 == 0)
					System.out.println(lineNum);
				lineNum++;
				line1 = br1.readLine();
				line2 = br2.readLine();
				token = line1 + "_" + line2;
				int index = lineClick.indexOf("\t");
				if (!clickInfo.containsKey(token)){
					clickInfo.put(token, (int)Double.parseDouble(lineClick.substring(0,index)));
					impInfo.put(token, (int)Double.parseDouble(lineClick.substring(index + 1)));
				}else{
					int oldclick = clickInfo.get(token);
					int oldimp = impInfo.get(token);
					clickInfo.put(token, oldclick + (int)Double.parseDouble(lineClick.substring(0,index)));
					impInfo.put(token, oldimp + (int)Double.parseDouble(lineClick.substring(index + 1)));
				}
			}
			
			br1.close();
			br2.close();
			cbr.close();
			
			BufferedWriter bw = new BufferedWriter(new FileWriter(query_userPath));
			Iterator<Entry<String, Integer>> iter = clickInfo.entrySet().iterator();
			while(iter.hasNext()){
				Entry<String, Integer> entry = iter.next();
				token = entry.getKey();
				int click = entry.getValue();
				int imp = impInfo.get(token);
				bw.write(String.valueOf(click));
				bw.write("\t");
				bw.write(String.valueOf(imp));
				bw.write("\t");
				bw.write(token);
				bw.write("\n");
			}
			
			bw.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void generateTruple(){
		try{
			BufferedReader br1 = new BufferedReader(new FileReader(queryTrainPath));
			BufferedReader br2 = new BufferedReader(new FileReader(userTrainPath));
			BufferedReader br3 = new BufferedReader(new FileReader(titleTrainPath));
			BufferedReader cbr = new BufferedReader(new FileReader(clickTrainPath));
			
			HashMap<String, Integer> clickInfo = new HashMap<String, Integer>();
			HashMap<String, Integer> impInfo = new HashMap<String, Integer>();
			
			String lineClick;
			String line1;
			String line2;
			String line3;
			String token;
			int lineNum = 0;
			while ((lineClick = cbr.readLine()) != null){
				if (lineNum % 1000000 == 0)
					System.out.println(lineNum);
				lineNum++;
				line1 = br1.readLine();
				line2 = br2.readLine();
				line3 = br3.readLine();
				token = line1 + "_" + line2 + "_" + line3;
				int index = lineClick.indexOf("\t");
				if (!clickInfo.containsKey(token)){
					clickInfo.put(token, (int)Double.parseDouble(lineClick.substring(0,index)));
					impInfo.put(token, (int)Double.parseDouble(lineClick.substring(index + 1)));
				}else{
					int oldclick = clickInfo.get(token);
					int oldimp = impInfo.get(token);
					clickInfo.put(token, oldclick + (int)Double.parseDouble(lineClick.substring(0,index)));
					impInfo.put(token, oldimp + (int)Double.parseDouble(lineClick.substring(index + 1)));
				}
			}
			
			br1.close();
			br2.close();
			br3.close();
			cbr.close();
			
			BufferedWriter bw = new BufferedWriter(new FileWriter(qutPath));
			Iterator<Entry<String, Integer>> iter = clickInfo.entrySet().iterator();
			while(iter.hasNext()){
				Entry<String, Integer> entry = iter.next();
				token = entry.getKey();
				int click = entry.getValue();
				int imp = impInfo.get(token);
				bw.write(String.valueOf(click));
				bw.write("\t");
				bw.write(String.valueOf(imp));
				bw.write("\t");
				bw.write(token);
				bw.write("\n");
			}
			
			bw.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
//		generateTrainFile();
//		generateTrainCase();
		generateTrainCombine(queryTrainPath, titleTrainPath, query_titlePath);
		generateTrainCombine(userTrainPath, titleTrainPath, user_titlePath);
	}

}
